package com.axzhengxin.platform.sync.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.db.Db;
import cn.hutool.db.ds.DSFactory;
import com.axzhengxin.platform.business.domain.BizLoanInfo;
import com.axzhengxin.platform.business.mapper.BizLoanInfoMapper;
import com.axzhengxin.platform.sync.ISyncDataService;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.Map;
import java.util.Optional;

/**
 * BizLoanInfoSyncServiceImpl
 *
 * @author jiangnan
 * @date 2021/4/7
 */
@Slf4j
@Service
public class BizLoanInfoSyncServiceImpl implements ISyncDataService {

    static final int ORDER = BizApproveInfoSyncServiceImpl.ORDER + 10;
    /**
     * 基于授信信息加载基础的放款信息
     */
    private static final String LOAD_BASE_LOAN_INFO_SQL = "select t.approve_id as loan_id, t.apply_id, t.approve_id, t.customer_id, t.customer_name, t.create_by, t.create_time from biz_approve_info t";
    /**
     * 从旧企业征信中心库中加载放款详情信息
     */
    private static final String LOAD_LOAN_INFO_FROM_ORACLE_SQL = "SELECT t.LOAN_AMOUNT, t.LOAN_TYPE, nvl(t.INTEREST_RATE, 0) as INTEREST_RATE, t.PAYMENT_DAY, t.PAYMENT_DUE_DAY FROM WARNING_UPLOAD t WHERE t.TRANSSERIAL_NO = ?";
    /**
     * 担保方式映射
     */
    private static final Map<String, String> GUARANTEE_TYPE_MAPPER = MapUtil.<String, String>builder()
            .put("抵押贷款", "0")
            .put("信用贷款", "2")
            .put("担保贷款", "3")
            .put("质押贷款", "1")
            .put("票据贴现", "2")
            .build();
    @Autowired
    private DataSource ecscDs;
    @Autowired
    private BizLoanInfoMapper bizLoanInfoMapper;

    @SneakyThrows
    @Override
    public void exec() {
        Db.use(ecscDs)
                .query(LOAD_BASE_LOAN_INFO_SQL, BizLoanInfo.class)
                .stream()
                .peek(this::fillWithWarningUpload)
                .forEach(loanInfo -> bizLoanInfoMapper.insertBizLoanInfo(loanInfo));
    }

    @SneakyThrows
    private void fillWithWarningUpload(BizLoanInfo bizLoanInfo) {
        String profile = SpringUtils.getActiveProfile();
        Db.use(DSFactory.get(profile))
                .query(LOAD_LOAN_INFO_FROM_ORACLE_SQL, bizLoanInfo.getApplyId())
                .stream()
                .findFirst()
                .ifPresent(entity -> {
                    bizLoanInfo.setLoanAmount(entity.getBigDecimal("LOAN_AMOUNT"));
                    bizLoanInfo.setLoanRatio(entity.getBigDecimal("INTEREST_RATE"));
                    bizLoanInfo.setLoanStart(entity.getStr("PAYMENT_DAY"));
                    bizLoanInfo.setLoanEnd(entity.getStr("PAYMENT_DUE_DAY"));
                    String loanType = Optional.ofNullable(entity.getStr("LOAN_TYPE")).orElse("0");
                    if (!GUARANTEE_TYPE_MAPPER.values().contains(loanType)) {
                        bizLoanInfo.setGuaranteeType(GUARANTEE_TYPE_MAPPER.getOrDefault(loanType, "0"));
                    }
                });
    }

    @Override
    public int getOrder() {
        return ORDER;
    }

}
